package com.uccart_project.ui.forms;

import com.uccart_project.controller.Controller_Application;
import java.awt.Graphics;
import javax.swing.ImageIcon;

public class UI_Login extends javax.swing.JFrame {

    /**
     * Referencia al controlador de la aplicacion.
     */
    private Controller_Application controller_Application;
    /*
     *  Variable para almacenar la imagen de fondo.
     */
    private ImageIcon fondo;

    /**
     * Constructor de la clase recive como parametro una referencia al
     * controlador de la aplicacion.
     *
     * @param controller_Application
     */
    public UI_Login(Controller_Application controller_Application) {
        this.controller_Application = controller_Application;
        fondo = new ImageIcon(getClass().getResource("/com/uccart_project/ui/iconos/fondo.png"));
        initComponents();


        ImageIcon img = new ImageIcon(getClass().getResource("/com/uccart_project/ui/splash/logo-uccart.png"));

        this.setIconImage(img.getImage());
    }

    /**
     * Metodo que limpia los campos de entrada del id de usuario y la clave.
     */
    public void resetInputs() {
        this.jTextFieldLoginUsuario.setText(null);
        this.jPasswordFieldLoginPassword.setText(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel(){
            @Override
            public void paint(Graphics grphcs) {
                //To change body of generated methods, choose Tools | Templates.
                setOpaque(false);

                grphcs.drawImage(fondo.getImage(), 0, 0, null); //no need for ImageObserver here
                super.paint(grphcs);
            }};
            ;
            jLabel3 = new javax.swing.JLabel();
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jTextFieldLoginUsuario = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jPasswordFieldLoginPassword = new javax.swing.JPasswordField();
            jButtonLoginEntrar = new javax.swing.JButton();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("UCCART FINANCIERO");
            setAlwaysOnTop(true);
            setBackground(new java.awt.Color(255, 255, 255));
            setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            setLocationByPlatform(true);
            setResizable(false);

            jPanel3.setBackground(new java.awt.Color(255, 255, 255));

            jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/uccart_project/ui/iconos/uccart2.png"))); // NOI18N
            jLabel3.setFocusTraversalKeysEnabled(false);

            jPanel1.setBackground(new java.awt.Color(255, 255, 255));
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Login"));
            jPanel1.setOpaque(false);

            jLabel1.setText("Usuario");

            jTextFieldLoginUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyTyped(java.awt.event.KeyEvent evt) {
                    jTextFieldLoginUsuarioKeyTyped(evt);
                }
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    jTextFieldLoginUsuarioKeyPressed(evt);
                }
            });

            jLabel2.setText("Contraseña");

            jPasswordFieldLoginPassword.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    jPasswordFieldLoginPasswordKeyPressed(evt);
                }
            });

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(41, 41, 41)
                            .addComponent(jTextFieldLoginUsuario))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(23, 23, 23)
                            .addComponent(jPasswordFieldLoginPassword)))
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jTextFieldLoginUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jPasswordFieldLoginPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            jButtonLoginEntrar.setBackground(new java.awt.Color(255, 255, 255));
            jButtonLoginEntrar.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
            jButtonLoginEntrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/uccart_project/ui/iconos/Enviar.png"))); // NOI18N
            jButtonLoginEntrar.setToolTipText("Iniciar Sesion");
            jButtonLoginEntrar.setBorder(null);
            jButtonLoginEntrar.setBorderPainted(false);
            jButtonLoginEntrar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            jButtonLoginEntrar.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/com/uccart_project/ui/iconos/Enviar1.png"))); // NOI18N
            jButtonLoginEntrar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    actionPerfomedLoginEntrar(evt);
                }
            });

            javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(18, 18, 18)
                            .addComponent(jButtonLoginEntrar)))
                    .addContainerGap())
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel3)
                    .addGap(18, 18, 18)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonLoginEntrar))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );

            getAccessibleContext().setAccessibleDescription("");

            pack();
            setLocationRelativeTo(null);
        }// </editor-fold>//GEN-END:initComponents

    private void actionPerfomedLoginEntrar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionPerfomedLoginEntrar
        this.controller_Application.verificarUsuario(jTextFieldLoginUsuario.getText(), String.valueOf(jPasswordFieldLoginPassword.getPassword()));
    }//GEN-LAST:event_actionPerfomedLoginEntrar

    private void jPasswordFieldLoginPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldLoginPasswordKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            this.controller_Application.verificarUsuario(jTextFieldLoginUsuario.getText(), String.valueOf(jPasswordFieldLoginPassword.getPassword()));
        }
    }//GEN-LAST:event_jPasswordFieldLoginPasswordKeyPressed

    private void jTextFieldLoginUsuarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldLoginUsuarioKeyTyped
        char c;
        c = evt.getKeyChar();
        if (c < '0' || c > '9') {
            evt.consume();//ignora el caracter digitado
        }
    }//GEN-LAST:event_jTextFieldLoginUsuarioKeyTyped

    private void jTextFieldLoginUsuarioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldLoginUsuarioKeyPressed

        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            this.controller_Application.verificarUsuario(jTextFieldLoginUsuario.getText(), String.valueOf(jPasswordFieldLoginPassword.getPassword()));
        }
    }//GEN-LAST:event_jTextFieldLoginUsuarioKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLoginEntrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPasswordField jPasswordFieldLoginPassword;
    private javax.swing.JTextField jTextFieldLoginUsuario;
    // End of variables declaration//GEN-END:variables
}
